Repository navigation
fix(js,ts): default imports follow a module's imported default; calls never land on types - #2800
colbymchenry wants to merge 1 commit into
Conversation
… never land on types Two resolver bugs that land an import on the wrong kind of symbol, on main before PR #2799 made them common (its A/B found them in jest, webdriverio and memfs): - A default import of a module whose `export default X` names a binding the module only imports fell back to a guess: the first function the module exports, or, through `export * from`, the first function of a forwarded module. jest-resolve (`import Resolver from './resolver'; export * from './utils'; export default Resolver`) sent 23 edges such as `Resolver.findNodeModule()` to `resolveTestEnvironment`. - A call or `new` reached a TypeScript interface or type alias: memfs's `Ok(...)` reached `interface Ok` beside `function Ok`, and `new Response()` / `new Worker()` linked to project types for those globals. Fix: - import-resolver.ts: a default naming an imported binding is that import, followed into its module (a namespace or outside package gives nothing, never a guess), and `export *` is never walked for a default, as ES modules never forward one. - gateTargetKind: a JS-family `calls`/`instantiates` result on an interface or type alias moves to the value its file declares under that name (function, class, component, constant, variable), else it is dropped. Validation: - __tests__/default-import-and-value-targets.test.ts (8 tests): 6 fail on main; the 2 that pass pin behaviour main gets right. All pass on the kernel. Each of 7 rules removed in turn fails at least one test. - Sync equals a fresh index (CLI) in 9 scenarios: defaults becoming imported or declared, a barrel gaining `export *`, a forwarded default moving, the value beside a type added or removed. - A/B on 14 TS/JS repos and 2 controls: jest 23 edges corrected to jest-resolve's Resolver; memfs 11 Ok/Err calls to their functions; webdriverio 2 allure calls to AllureReporter's methods; outline's 2 Sentry calls off a guessed function; 165 `new` of globals dropped (vite 27, nuxt 57, webdriverio 26, insomnia 47, jest 7, memfs 1) plus jest's call to `type write`. vben, t3-turbo, nitro, vue3-antdv-admin, pathe, c12, flask and gin byte-identical. - Cost: fresh index within noise (median of 3, interleaved). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Held for Colby, not merged: the default-export half of this PR overlaps #2754, which is waiting on his review (it replaces #2451, which he asked to keep open and unmerged). Specifically, following an |
|
The Everything below was verified on head After a barrel that forwards an imported default is edited, sync keeps the old edge. // real.ts
export default function real() {}// b.ts
import D from './real';
export default D;// use.ts
import B from './b';
export function u() { B(); }
On the rebase. Main's What holds. Aliased, Checked against the fixtures in graph-assay, which I maintain: the pr-2800 fixture reads BROKEN on the merge base and on main, and FIXED on this head. The pr-2754 fixture stays BROKEN but loses one wrong edge ( |
Summary
Two resolver bugs that land an import on the wrong kind of symbol. Both are older than #2799 and were already on main, but #2799 made them common, because packages imported by name now resolve. Its A/B showed them in jest, webdriverio and memfs, and they need no workspace to reproduce.
import Resolver from './resolver'; export * from './utils'; export default Resolver. A default import of it walked theexport *and took the first functionutils.tsexports, so 23 jest edges such asResolver.findNodeModule(),new Resolver()andResolvertypes landed onresolveTestEnvironment. A module that default-exports an import of its own beside its own functions had the same problem: insomnia'smain/log.ts(import log from 'electron-log/main'…export default log) and outline'slogging/sentry.tssent their importers'log.info()andSentry.captureException()calls to whichever function the module exported first.newlanding on a TypeScript interface or type alias. memfs'sresult.tsdeclaresinterface Ok<T>andfunction Ok<T>(), andOk(…)through an import reached the interface. Anewof a global that a project only types (new Response(),new Worker(),new Event()) was linked by name to the project'sinterface Responseortype Event.Root cause
findExportedSymbolWalk(import-resolver.ts) answers a default import from the module's ownexport defaultstatement (#2432). It missed two things.export * fromre-exports for the default too. An ES module'sexport *never forwardsdefault.The resolver has no rule on what a call may target. Whichever strategy found the name (import, then name match) chose among same-named nodes by order, and nothing stopped it on a type.
Fix
import-resolver.tsstatedDefaultExport: a default that names a binding the module imports and doesn't declare is now that import ({ imported }), read from the module's import mappings.esmDefaultExportfollows that import into the module it comes from, as a named or default import of that module. Nothing comes back for a namespace import or a package outside the project, and no guess is made.findExportedSymbolWalk:export *for a default (Python's star imports are untouched).index.ts(gateTargetKind): for the JS family, acallsorinstantiatesresult whose target is aninterfaceor atype_aliasmoves to the value the target's file declares under the same name (a function, class, component, constant or variable, first by position). That coversfunction Okbesideinterface Ok, and a class merged with an interface. With no such value, the result is dropped, sonew Response()links nothing.Overlap with #2754, which is open and held: following a default that names an import is one of the things #2754 does, implemented here minimally (
{ imported }inStatedDefault, one follow). #2754's broader reading (wrappers,new X(),export { X as default }, forwarded named exports) is untouched, and would supersede this part when it lands. Theexport *rule and the call/newgate are not in #2754.Validation
Tests:
__tests__/default-import-and-value-targets.test.tsindexes real temp repositories (8 tests).export *(jest-resolve's shape);node.ts);export *;electron-log, a@sentry/nodenamespace) beside the module's own functions;Ok(…)reachesfunction Okbesideinterface Ok, directly and through a barrel, while a type position still names the interface;new Response()andnew Event()link nothing when only project types share the names;new Store()reaches the class an interface merges with.Results:
export *walk.export *and defaults, the imported default, following it, the call/newgate, the value beside the type,newgated, and a class beside an interface.Sync equals a fresh index: CLI
codegraph syncagainst a freshinit, in 9 scenarios:export *;A/B: fresh
init, both arms with the native kernel, site-paired edge diff, main e8ddc4e.Resolver.findNodeModule()and the other static calls,new Resolver()andResolvertypes now reach jest-resolve'sResolverclass through the import. They had been onresolveTestEnvironment; before #2799 main had them right, but only by name match. 8 removed:new Event(),new Module()and anew Reporter()of a local variable, which had landed on type aliases and interfaces, andwrite()ontype write.Ok(…)/Err(…)calls move frominterface Ok/Errto the functions.new ReadableStream()no longer lands on a project interface.allure.addFeature()/addDescription()calls move fromapi.ts's first function toAllureReporter's methods. 55new SauceService()keep their target, now through the import. 26newof globals are dropped (Event,Response,Request, mocha'sContextandSuite).new Response(),new Request(),new Headers()andnew Event()are dropped; they had linked to Postman importer types. 2log.info()calls on electron-log's logger move frominitializeLoggingto a method-name guess,Console.info. That is wrong both before and after (see below).Sentry.withScope()/captureException()calls no longer land onrequestErrorHandler.BackgroundNotificationHandler.setBadgeCountAsync()calls now link the constant the module's default forwards, as a named import of it already does, instead of the type's method guessed by name. 2new BroadcastChannel()keep their target, now through the import.new Worker()/new Event()dropped; they had linked to.d.tsinterfaces.new Response()dropped; it had linked to atype Responsein a build stub.I checked every removed
newby grouping on name and target. Each names a global (Worker,Event,Response,Request,Headers,ReadableStream, Node'sModule, mocha'sContext/Suite) or a local variable, never a class of the project.Cost: fresh index, median of 3, run back to back, alternating which arm goes first. All three are within noise:
The added work is one import-mapping lookup per default statement that names a binding, and one same-file lookup when a call target is a type.
Full suite on 48f0d88 (
CODEGRAPH_KERNEL_EXPECT=1, 4 workers): 725 files (719 passed, 6 skipped), 8,693 tests: 8,611 passed, 0 failed, 82 skipped. No file needed a rerun.Not in this PR
export const ModuleMap = HasteModuleMap as …andexport const TestEnvironment = JSDOMEnvironmentare still not followed to the class. jest's 26ModuleMap.create()calls link the constant, and 6extends require('jest-environment-jsdom').TestEnvironmentstay unlinked. Following a constant's initializer to a class, through casts, is its own change.import log from './main/log', wherelog.tsforwardselectron-log's default, thenlog.info(): the call no longer reaches a guessed function oflog.ts. It is still guessed by method name elsewhere (insomnia:Console.info), because the receiver isn't known to come from an outside package. A namespace default like outline'sSentrylinks nothing.🤖 Generated with Claude Code